java 网络编程

Posted by ZhengYang on 2016-09-19

网络编程常用的class

  • java.net.DatagramPacket
  • java.net.DatagramSocket
  • java.net.InetAddress
    • java.net.Inet4Address
    • java.net.Inet6Address
  • java.net.ServerSocket
  • java.net.ServerSocket

UDP-101

用户数据报协议 UDP(User Datagram Protocol),是无连接的,即不可靠传输。
先运行UDPRecv,运行的程序处于阻塞状态,直到有数据接收到为止,即UDPSend运行后,UDPRecv接收到数据后才停止。

  1. 先运行UDPRecv
  2. 再运行UDPSend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.IOException;
import java.net.*;;
public class UDPRecv {
public static void main(String[] args) throws IOException{
// 创建DatagramSocket对象,有端口参数
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
// 创建DatagramPacket,只需要buf
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
ds.close();
String strRecv = new String(dp.getData(), 0, dp.getLength());
System.out.println(strRecv); // 发送方发送的 信息
System.out.println(dp.getAddress().getHostAddress()); // 发送方ip
System.out.println(dp.getPort()); // 发送方port
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.*;
import java.net.*;
public class UDPSend {
public static void main(String[] args) throws IOException{
// 创建DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
String str = "hello!";
byte[] buf = str.getBytes();
// 创建DatagramPacket对象,需要buf,ip,port
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"),3000);
ds.send(dp); // 发送数据
ds.close();
}
}

TCP-101

传输控制协议 TCP(Transmission Control Protocol),是面向连接的,可靠传输。
先运行TCPServer,运行的程序处于阻塞状态,直到有数据接收到为止,即TCPClient运行后,TCPServer接收到数据后才停止。

  1. 先运行TCPServer
  2. 再运行TCPClient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
public class TCPServer {
public static void main(String[] args) {
try{
// ServerSocket对象 (端口,最大连接数,ip)
ServerSocket ss = new ServerSocket(8001, 3, InetAddress.getByName("10.3.42.166"));
// 接收到一个socket
Socket s = ss.accept();
// ops发送内容给对方
OutputStream ops = s.getOutputStream();
String str = "hello "+s.getInetAddress().getHostAddress();
ops.write(str.getBytes());
// ips取得对方发送过来的内容
InputStream ips = s.getInputStream();
byte[] buf = new byte[1024];
int len = ips.read(buf);
System.out.println(new String(buf,0,len));
ips.close();
ops.close();
s.close();
ss.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
public class TCPClient {
public static void main(String[] args){
try{
// Socket对象 (ip, 端口)
Socket s = new Socket(InetAddress.getByName("10.3.42.166"),8001);
// ops发送内容给对方
OutputStream ops = s.getOutputStream();
String str = "from client "+s.getInetAddress().getHostAddress();
ops.write(str.getBytes());
// ips接受对方发送过来的数据
InputStream ips = s.getInputStream();
byte[] buf = new byte[1024];
int len = ips.read(buf);
System.out.println(new String(buf,0,len));
ops.close();
s.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

Tcp协议保证收发双方正确地数据传送,但没法保证接受方正确地理解发送方的数据的意义。
在Tcp上有各种网络应用,有的用于在两个程序之间传送邮件(stmp和pop3),

  • 有的是在两个程序之间传送文件(ftp)
  • 有的是在两个程序之间传送www网页(http)

只有基于这些协议,接收方才能正确理解发送方的数据。
总之,把Tcp协议比作电话,把各种网络应用协议(ftp,smtp,pop3,http等)比作各种语言。
当然,我们的语言不一定非要通过电话系统传递一样的道理,我们的应用程序协议也可以在其他的网络通信协议(非Tcp协议)上传送。